﻿using System;
using System.IO;
using System.Configuration;
using System.Text.RegularExpressions;

namespace Hxsoft.AppV1
{
	public static class UploadRule
	{
		public const string FileTypeSeparator = "|";
		public const string UploadType_Image = "image";
		private const string UploadImageType = "gif|jpg|jpeg|png|swf|bmp|jpeg2000|tiff|psd|ico|svg|pcx|dxf|wmf|emf|lic|eps|tga|tif";		// 暂时写成常量，必要时将替换为配置
		
		public static string GetAllowUploadResourceType()
		{
			if (ApplicationConfiguration.Configuration != null && ApplicationConfiguration.Configuration.UploadRule != null)
				return ApplicationConfiguration.Configuration.UploadRule.AllowUploadResourceType;
			return null;
		}

		public static long? GetUploadResourceMaxSize()
		{
			if (ApplicationConfiguration.Configuration != null && ApplicationConfiguration.Configuration.UploadRule != null)
				return ApplicationConfiguration.Configuration.UploadRule.UploadResourceMaxSize;
			return null;
		}

		/// <summary>
		/// 验证指定项目指定内嵌资源的文件尺寸是否是允许的范围。
		/// </summary>
		/// <param name="length">资源的尺寸。</param>
		/// <returns>布尔值表示指定项目指定内嵌资源的文件尺寸是否是允许的范围。</returns>
		public static bool IsAllowUploadResourceSize(long length)
		{
			long? maxSize = GetUploadResourceMaxSize();
			if(maxSize.HasValue)
				return length <= maxSize.Value;
			return true;
		}

		public static bool IsAllowUploadResourceType(string uploadType)
		{
			return IsAllowUploadType(GetAllowUploadResourceType(), uploadType);
		}

		/// <summary>
		/// 获取过滤资源类别。
		/// </summary>
		/// <param name="name">过滤的名称。</param>
		/// <returns>过滤资源类别。</returns>
		public static string GetFilterResourceType(string name)
		{
			switch (name.ToLower())
			{
				case "image":
					return UploadImageType;
			}
			return null;
		}

		public static string GetFilterUploadResourceType(string filterType)
		{
			string uploadType = GetAllowUploadResourceType();
			if (string.IsNullOrEmpty(uploadType)) return null;
			if (string.IsNullOrEmpty(filterType)) return null;

			return uploadType.Split(FileTypeSeparator, StringSplitOptions.RemoveEmptyEntries).IntersectSet(filterType.Split(FileTypeSeparator, StringSplitOptions.RemoveEmptyEntries)).Join(FileTypeSeparator);
		}

		/// <summary>
		/// 确定要上传的文件类型是否为允许的文件类型。
		/// </summary>
		/// <param name="allowUploadType">允许的文件类型列表。</param>
		/// <param name="uploadType">要上传的文件类型。</param>
		/// <returns>要上传的文件类型是否为允许的文件类型。</returns>
		public static bool IsAllowUploadType(string allowUploadType, string uploadType)
		{
			bool isAllow = false;

			if (!string.IsNullOrEmpty(allowUploadType))
			{
				if (Regex.IsMatch(uploadType, "^(" + allowUploadType + ")$", RegexOptions.IgnoreCase))
					isAllow = true;
			}

			return isAllow;
		}

		/// <summary>
		/// 取得文件类型。
		/// </summary>
		/// <param name="fileName">文件的名称。</param>
		/// <returns>文件类型。</returns>
		public static string GetFileType(string fileName)
		{
			if (string.IsNullOrEmpty(fileName))
				return null;

			int position = fileName.LastIndexOf('.');
			if (position == -1)
				return null;
			return fileName.Substring(position + 1);
		}

		/// <summary>
		/// 取得文件名。
		/// </summary>
		/// <param name="filePath">文件的路径。</param>
		/// <returns>文件名。</returns>
		public static string GetFileFullName(string filePath)
		{
			if (string.IsNullOrEmpty(filePath))
				return null;

			int position = filePath.LastIndexOf('\\');
			if (position == -1)
				return filePath;
			return filePath.Substring(position + 1);
		}

		/// <summary>
		/// 取得文件名。
		/// </summary>
		/// <param name="filePath">文件的路径。</param>
		/// <returns>文件名。</returns>
		public static string GetFileName(string filePath)
		{
			string fileName = GetFileFullName(filePath);

			int position = filePath.LastIndexOf('.');
			if (position == -1)
				return filePath;
			return filePath.Substring(0, position);
		}

		/// <summary>
		/// 复制流内容。
		/// </summary>
		/// <param name="source">复制源。</param>
		/// <param name="target">复制目标。</param>
		public static void CopyStream(Stream source, Stream target)
		{
			const int BatchReadCount = 1000;

			if (source.CanSeek) source.Position = 0;
			if (source.CanSeek) target.Position = 0;

			byte[] content = new byte[BatchReadCount];
			int readCount = source.Read(content, 0, BatchReadCount);
			while (readCount > 0)
			{
				target.Write(content, 0, readCount);
				readCount = source.Read(content, 0, BatchReadCount);
			}
		}
	}
}